//
//  CMNotification.swift
//  covermedia
//
//  Created by travel on 16/6/29.
//  Copyright © 2016年 covermedia. All rights reserved.
//

import Foundation

public enum CMNotification {

    case topicSubscribeStateChange
    
    case commentAddComment
    case commentAddCommentLocal      //本地通知
    case commentDelComment           //删除评论通知
    case commentPraiseComment        //评论点赞通知
    
    case coverAccountSub             //封面号订阅广播
    
    case loginWechat
    case loginWeibo
    case loginLoginSusscess
    case loginout
    
    case channelListUpdateNumber
    case channelSubscribeUpdate
    case channelGotoChannel
    case channelRefreshRecommendList
    
    case updateUserInfo

    case settingChangeFontSize
    
    case shareSuccess
    
    case imageGallaryDismiss
    
    
    case mainShowLeftPannel
    
    case badgeNumberUpdate
    
    case liveBookUpdate             //预告预约通知
    case payfinished            // 支付完成通知
    
    public var name:String {
        return "\(self)"
    }
    
    
    

}

public func == (a:Notification.Name, b:CMNotification) -> Bool {
    return a.rawValue == b.name
}

public func == (a:CMNotification, b:Notification.Name) -> Bool {
    return a.name == b.rawValue
}

extension CMNotification {
    public static func addObserver(_ observer: AnyObject, selector aSelector: Selector, name aName: CMNotification, object anObject: AnyObject?) {
        NotificationCenter.default.addObserver(observer, selector: aSelector, name: Notification.Name(rawValue: aName.name), object: anObject)
    }
    
    public static func postNotification(_ notification: Notification) {
        NotificationCenter.default.post(notification)
    }
    public static func postNotificationName(_ aName: CMNotification, object anObject: Any?) {
        NotificationCenter.default.post(name: Notification.Name(rawValue: aName.name), object: anObject)
    }
    public static func postNotificationName(_ aName: CMNotification, object anObject: Any?, userInfo aUserInfo: [AnyHashable: Any]?) {
         NotificationCenter.default.post(name: Notification.Name(rawValue: aName.name), object: anObject, userInfo: aUserInfo)
    }
    
    public static func removeObserver(_ observer: AnyObject) {
        NotificationCenter.default.removeObserver(observer)
    }
    public static func removeObserver(_ observer: AnyObject, name aName: CMNotification?, object anObject: AnyObject?) {
        NotificationCenter.default.removeObserver(observer, name: (aName?.name).map { Notification.Name(rawValue: $0) } ?? nil, object: anObject)
    }
}

extension CMNotification {
    public static func addObserver(_ observer: AnyObject, selector aSelector: Selector, name aName: Notification.Name, object anObject: AnyObject?) {
        NotificationCenter.default.addObserver(observer, selector: aSelector, name: aName, object: anObject)
    }
    
    public static func postNotificationName(_ aName: String, object anObject: Any?) {
        NotificationCenter.default.post(name: Notification.Name(rawValue: aName), object: anObject)
    }
}


extension CMNotification {
    public static func notification(_ name: CMNotification, object: Any?, userInfo: [AnyHashable: Any]? = nil) -> Notification {
        if let userInfo = userInfo {
            return Notification.init(name: Notification.Name(rawValue: name.name), object: object, userInfo: userInfo)
        }
        return Notification.init(name: Notification.Name(rawValue: name.name), object: object)
    }
    
    public func notification(_ object: Any?, userInfo: [AnyHashable: Any]?) -> Notification {
        return Notification.init(name: Notification.Name(rawValue: name), object: object, userInfo: userInfo)
    }
}

extension Notification {
    public func post() {
        NotificationCenter.default.post(self)
    }
}
